<template>
  <div class="java-editor">
    <div class="editor-container">
      <div class="left-panel">
        <div class="panel-header">
          <h3>方法列表</h3>
          <span class="method-count" v-if="methods.length > 0">({{ methods.length }})</span>
        </div>
        <div class="method-list" v-if="methods.length > 0">
          <div
            v-for="method in methods"
            :key="method.name + method.line"
            class="method-item"
            :class="{
              active: currentMethod === method.name,
              'public-method': method.modifier.includes('public'),
              'private-method': method.modifier.includes('private'),
              'protected-method': method.modifier.includes('protected'),
              'static-method': method.modifier.includes('static')
            }"
            @click="jumpToMethod(method)"
            @mouseenter="hoverMethod = method.name"
            @mouseleave="hoverMethod = ''"
          >
            <span class="method-icon">{{ getMethodIcon(method) }}</span>
            <div class="method-info">
              <span class="method-name">{{ formatMethodDisplay(method) }}</span>
              <span class="method-details">{{ getMethodDetails(method) }}</span>
            </div>
            <span class="method-line">第{{ method.line }}行</span>
          </div>
        </div>
        <div class="empty-state" v-else>
          <div class="empty-icon">📋</div>
          <p>暂无方法</p>
          <small>输入Java代码后将自动解析方法</small>
        </div>
      </div>
      
      <div class="right-panel">
        <div class="editor-header">
          <span class="file-name">{{ fileName }}</span>
        </div>
        <div class="code-container">
          <textarea 
            ref="codeTextarea"
            v-model="javaCode" 
            class="code-textarea"
            @input="onCodeChange"
            spellcheck="false"
          ></textarea>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import JavaFileParser from './JavaFileParser.js'

export default {
  name: 'SimpleJavaEditor',
  data() {
    return {
      fileName: 'UserManager.java',
      javaCode: '',
      allMethods: [],
      methods: [],
      currentMethod: '',
      currentLine: 0,
      hoverMethod: '',
      parser: null
    }
  },
  mounted() {
    this.parser = new JavaFileParser()
    this.loadDefaultFile()
  },
  methods: {
    loadDefaultFile() {
      this.javaCode = `import java.util.*;

/**
 * 示例Java类 - 用户管理系统
 */
public class UserManager {
    private List<User> users;
    private Map<String, User> userMap;
    private static final int MAX_USERS = 1000;
    
    public UserManager() {
        this.users = new ArrayList<>();
        this.userMap = new HashMap<>();
    }
    
    /**
     * 添加用户
     */
    public boolean addUser(User user) {
        if (users.size() >= MAX_USERS) {
            return false;
        }
        users.add(user);
        userMap.put(user.getId(), user);
        return true;
    }
    
    /**
     * 根据ID删除用户
     */
    public boolean deleteUser(String userId) {
        User user = userMap.remove(userId);
        if (user != null) {
            users.remove(user);
            return true;
        }
        return false;
    }
    
    /**
     * 更新用户信息
     */
    public boolean updateUser(String userId, String newName, int newAge) {
        User user = userMap.get(userId);
        if (user != null) {
            user.setName(newName);
            user.setAge(newAge);
            return true;
        }
        return false;
    }
    
    /**
     * 根据ID查找用户
     */
    public User findUserById(String userId) {
        return userMap.get(userId);
    }
    
    /**
     * 获取所有用户
     */
    public List<User> getAllUsers() {
        return new ArrayList<>(users);
    }
    
    /**
     * 按年龄排序用户
     */
    public List<User> getUsersSortedByAge() {
        List<User> sortedUsers = new ArrayList<>(users);
        sortedUsers.sort(Comparator.comparingInt(User::getAge));
        return sortedUsers;
    }
    
    /**
     * 获取用户数量
     */
    public int getUserCount() {
        return users.size();
    }
    
    /**
     * 清空所有用户
     */
    public void clearAllUsers() {
        users.clear();
        userMap.clear();
    }
    
    /**
     * 验证用户数据
     */
    private boolean validateUser(User user) {
        return user != null && 
               user.getName() != null && 
               !user.getName().trim().isEmpty() &&
               user.getAge() > 0 && 
               user.getAge() < 150;
    }
    
    /**
     * 生成用户报告
     */
    public String generateUserReport() {
        StringBuilder report = new StringBuilder();
        report.append("用户报告\\n");
        report.append("========\\n");
        report.append("总用户数: ").append(users.size()).append("\\n");
        report.append("用户列表:\\n");
        
        for (User user : users) {
            report.append("- ").append(user.getName())
                  .append(" (").append(user.getAge()).append("岁)\\n");
        }
        
        return report.toString();
    }
}`
      this.fileName = 'UserManager.java'
      this.parseJavaCode()
    },

    onCodeChange() {
      this.parseJavaCode()
    },

    parseJavaCode() {
      if (this.javaCode && this.parser) {
        const result = this.parser.parse(this.javaCode)
        this.allMethods = result.methods
        this.methods = result.methods.map(method => ({
          ...method,
          displayName: this.formatMethodDisplay(method)
        }))
      }
    },

    formatMethodDisplay(method) {
      const params = method.parameters.map(p => `${p.type} ${p.name}`).join(', ')
      return `${method.name}(${params})`
    },

    getMethodIcon(method) {
      if (method.modifier.includes('public')) return '🔓'
      if (method.modifier.includes('private')) return '🔒'
      if (method.modifier.includes('protected')) return '🔐'
      return '🔧'
    },

    getMethodDetails(method) {
      const modifier = method.modifier || 'package-private'
      const returnType = method.returnType || 'void'
      return `${modifier} • ${returnType}`
    },

    jumpToMethod(method) {
      this.currentMethod = method.name
      this.currentLine = method.line
      this.jumpToLine(method.line)
    },

    jumpToLine(lineNumber) {
      if (this.$refs.codeTextarea) {
        const lines = this.javaCode.split('\n')
        let charCount = 0
        for (let i = 0; i < lineNumber - 1; i++) {
          charCount += lines[i].length + 1 // +1 for newline
        }
        this.$refs.codeTextarea.focus()
        this.$refs.codeTextarea.setSelectionRange(charCount, charCount)
        this.$refs.codeTextarea.scrollTop = (lineNumber - 1) * 20 // 假设每行20px
      }
    }
  }
}
</script>

<style scoped>
.java-editor {
  width: 100%;
  height: 100vh;
  font-family: 'Consolas', 'Monaco', monospace;
}

.editor-container {
  display: flex;
  height: 100%;
}

.left-panel {
  width: 320px;
  background-color: #f8f9fa;
  border-right: 1px solid #dee2e6;
  padding: 20px;
  overflow-y: auto;
}

.left-panel h3 {
  margin: 0 0 20px 0;
  color: #495057;
  font-size: 18px;
  font-weight: 600;
  padding-bottom: 10px;
  border-bottom: 2px solid #007bff;
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 2px solid #007bff;
}

.panel-header h3 {
  margin: 0;
  color: #495057;
  font-size: 18px;
  font-weight: 600;
}

.method-count {
  background-color: #007bff;
  color: white;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}

.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: #6c757d;
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 15px;
  opacity: 0.5;
}

.empty-state p {
  margin: 0 0 8px 0;
  font-size: 16px;
  font-weight: 500;
}

.empty-state small {
  font-size: 12px;
  opacity: 0.7;
}

/* 自定义滚动条样式 */
.method-list::-webkit-scrollbar {
  width: 6px;
}

.method-list::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 3px;
}

.method-list::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 3px;
}

.method-list::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}

.method-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-height: calc(100vh - 200px);
  overflow-y: auto;
  padding: 5px 0;
}

.method-item {
  display: flex;
  align-items: flex-start;
  padding: 10px 12px;
  background-color: white;
  border: 1px solid #e0e0e0;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s ease;
  position: relative;
  overflow: hidden;
}

.method-item:hover {
  background-color: #f8f9fa;
  border-color: #007bff;
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(0, 123, 255, 0.15);
}

.method-item.active {
  background-color: #007bff;
  color: white;
  border-color: #0056b3;
  box-shadow: 0 2px 8px rgba(0, 123, 255, 0.3);
}

.method-item.active:hover {
  background-color: #0056b3;
}

.method-item.public-method {
  border-left: 3px solid #28a745;
}

.method-item.private-method {
  border-left: 3px solid #dc3545;
}

.method-item.protected-method {
  border-left: 3px solid #ffc107;
}

.method-item.static-method {
  background-color: #f8f9fa;
}

.method-icon {
  margin-right: 10px;
  font-size: 14px;
  margin-top: 2px;
}

.method-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.method-name {
  font-weight: 600;
  font-size: 13px;
  line-height: 1.3;
}

.method-details {
  font-size: 11px;
  opacity: 0.8;
  font-weight: 400;
}

.method-line {
  font-size: 11px;
  opacity: 0.6;
  font-weight: 500;
}

.method-item.active .method-details,
.method-item.active .method-line {
  opacity: 0.9;
}

.right-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  background-color: white;
  overflow: hidden;
}

.editor-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 20px;
  background-color: #fafafa;
  border-bottom: 1px solid #e0e0e0;
}

.file-name {
  font-weight: 600;
  color: #333;
}

.code-container {
  flex: 1;
  overflow: auto;
  background-color: #ffffff;
}

.code-textarea {
  width: 100%;
  height: 100%;
  border: none;
  outline: none;
  font-family: 'Consolas', 'Monaco', monospace;
  font-size: 14px;
  line-height: 1.5;
  color: #333;
  background-color: #ffffff;
  padding: 15px;
  resize: none;
  tab-size: 4;
}
</style>